package org.grottarossa.ui.core.container;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.workflows.Workflow;
import org.grottarossa.template.data.pojo.workflows.WorkflowList;
import org.grottarossa.ui.core.i18n.DM;


import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public class WorkflowContainer extends IndexedContainer {

	/**
	 *
	 */
	private static final long serialVersionUID = 7952829449813614293L;

	WorkflowList workflowList = null;
	private ResourceBundle m_localizer = DM.instance().getBundle(getClass().getName());

	public WorkflowContainer(WorkflowList wList)
	{
		//create my table
		this.workflowList = wList;

		addContainerProperty("WorkflowId",Integer.class,null);
		addContainerProperty("WorkflowVersion",Integer.class,null);
		addContainerProperty("WorkflowName",String.class,null);
		addContainerProperty("WorkflowCaption",String.class,null);
		addContainerProperty("WorkflowStartActivity",String.class,null);
		addContainerProperty("WorkflowName",String.class,null);

		String sWorkflowCaption = "";

		for ( Workflow workflow : workflowList )
		{
			Object obj = addItem();
			Item it = getItem(obj);

			it.getItemProperty("WorkflowId").setValue(workflow.getWorkflowId().getIdentity());
			it.getItemProperty("WorkflowVersion").setValue(workflow.getWorkflowVersion());
			it.getItemProperty("WorkflowName").setValue(workflow.getWorkflowName());
			it.getItemProperty("WorkflowStartActivity").setValue(workflow.getWorkFlowStartActivity());
			it.getItemProperty("WorkflowName").setValue(workflow.getWorkflowName());


			try {
				sWorkflowCaption = m_localizer.getString(String.format("menu.WF.%s.caption", workflow.getWorkflowName()));
			} catch(Exception ex) {
				sWorkflowCaption = workflow.getWorkflowName();
			}

			if ( sWorkflowCaption == null)
				sWorkflowCaption = String.format("%s(%d)",workflow.getWorkflowName(), workflow.getWorkflowVersion());

			it.getItemProperty("WorkflowCaption").setValue(sWorkflowCaption);
		}
	}

}
